import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

/**
 * This class is Return check assignment UI
 */
public class ReturnCheckAssUI extends JPanel {
	
	/**
	 * title for the current window  
	 */
	private JLabel checkTitle = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * back help
	 */
	public JButton btnHelp = null;
	/**
	 * browse button
	 */
	public JButton btnBrowse= null;
	/**
	 * submit button
	 */
	public JButton btnSubmit = null;
	/**
	 *  submit button
	 */
	private JLabel lblGrade = null;
	/**
	 * comment label
	 */
	private JLabel lblComment = null;
	private JTextField TextGrade = null;
	private JTextPane TextComment = null;
	private boolean submitButtonEnabledFlg;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	private Integer grade;
	private String comment;
	private Sub sub;
	/**
	 * loaded file
	 */
	private MyFile file;
	/***
	 * 
	 * @param sub
	 */
	public ReturnCheckAssUI(Sub sub) {
		
		  super();
		  this.sub = sub;
		  initialize();
	}

	private void initialize() {
		curPanel = this;
		checkTitle = new JLabel("Check Assignment");
		checkTitle.setBounds(180, 28, 150, 14);

		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(checkTitle);
		
		submitButtonEnabledFlg = false;
		
		lblComment = new JLabel("Comment:");
		lblComment.setBounds(74, 100, 110, 14);
		this.add(lblComment);	
			
		TextComment = new JTextPane();
		TextComment.setBounds(new Rectangle(135, 100, 169, 92));
		TextComment.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				comment=TextComment.getText();			
			}
		}
		);
		this.add(TextComment,null);
		
		lblGrade = new JLabel("Grade:");
		lblGrade.setBounds(74, 71, 110, 14);
		this.add(lblGrade);	
			
		TextGrade = new JTextField();
		TextGrade.setBounds(new Rectangle(135, 66, 68, 25));
		TextGrade.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				grade = Integer.parseInt(TextGrade.getText());
			}
		}
		);
		this.add(TextGrade,null);
		

			
				//JButton Sub
				btnSubmit = new JButton("Submit");
				curPanel.add(btnSubmit);
				btnSubmit.setBounds(187, 216, 86, 25);
				btnSubmit.setEnabled(false);
				
				//btnSubmit.setEnabled(submitButtonEnabledFlg);
				btnSubmit.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						sub.setGrade(grade);
						sub.setComment(comment);
						TextGrade.setText(null);
						TextComment.setText(null);
						SQL s4 = new SQL (sub,49);
						s4.accept();
						if (s4.getMsg().equals(true))
						{
							MsgWin msgWin = new MsgWin("Your Check Assignment sent"); 	
						}
					}
				});
				this.add(btnSubmit,null);
				this.setVisible(true);
				
				
				//JButton Browse
				btnBrowse = new JButton("Browse");
				curPanel.add(btnBrowse);
				btnBrowse.setBounds(218, 66, 86, 25);
				btnBrowse.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
							Browse b = new Browse();
							file = b.getFile();
							String n = file.getName();
							String fileName ="check"+ sub.getCourseID()+"_"+sub.getAssNum().toString()+"_"+sub.getStudentID()+"_"+n;
							file.setName(fileName);
							submitButtonEnabledFlg = true;
							btnSubmit.setEnabled(true);
							System.out.println("file is" +file.getName()); 
				
				
				
							if (file != null)
							{
								sub.setCheckFile(file);
								sub.setGrade(grade);
								sub.setComment(comment);
							}
				
			
					}
				});
				this.add(btnBrowse,null);
				this.setVisible(true);
				
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose a Course and and click on ", "'Open Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);

	}
}
